package igou.togogo.controller;

import igou.togogo.bean.BuyCart;
import igou.togogo.bean.BuyItem;
import igou.togogo.param.CartParam;
import igou.togogo.pojo.EgouResult;
import igou.togogo.service.ICartService;
import igou.togogo.util.CookieUtils;
import igou.togogo.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/shopping-cart")
public class CarController {

    @Autowired
    ICartService cartService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public EgouResult add(@RequestBody CartParam param, HttpServletRequest request, HttpServletResponse response){
        //从cookie获取购物车
        String cartCookie = CookieUtils.getCookieValue(request, CookieUtils.BUYCART_COOKIE);
        BuyCart buyCart = cartService.add(cartCookie, param);
        if (buyCart !=null){
            //把购物车加入到cookie
            cartCookie = JsonUtils.objectToJson(buyCart);
            CookieUtils.setCookie(request,response,CookieUtils.BUYCART_COOKIE,cartCookie);
            return EgouResult.ok();
        }
        return EgouResult.build(400,"添加购物车失败!");
    }
    @RequestMapping
    public  String toCart(Model model,HttpServletRequest request){
        String cartCookie = CookieUtils.getCookieValue(request, CookieUtils.BUYCART_COOKIE);
        BuyCart buyCart = JsonUtils.jsonToPojo(cartCookie, BuyCart.class);
        model.addAttribute("buyCart",buyCart);
        return "/front_page/product/cart";
    }

    @RequestMapping("/clear")
    public String clearBuyCart(HttpServletRequest request,HttpServletResponse response){
        CookieUtils.setCookie(request,response,CookieUtils.BUYCART_COOKIE,null);
        return "/front_page/product/cart";
    }

    private BuyCart removeItem(HttpServletRequest request,HttpServletResponse response,Integer skuId){
        //从购物车中读取购物信息
        String cartCookie = CookieUtils.getCookieValue(request, CookieUtils.BUYCART_COOKIE);
        BuyCart buyCart = JsonUtils.jsonToPojo(cartCookie, BuyCart.class);
        //删除一个商品
        List<BuyItem> items = buyCart.getItems();
        BuyItem item = null;
        for (BuyItem temp : items){
            if (temp.getSku().getId().equals(skuId)){
                item = temp;
            }
        }
        if (item !=null){
            items.remove(item);
        }
        //重新加载购物车信息
        String buyCartJson = JsonUtils.objectToJson(buyCart);
        CookieUtils.setCookie(request,response,CookieUtils.BUYCART_COOKIE,buyCartJson);
        return buyCart;
    }

    /*移除*/
    @RequestMapping("/remove/{sku_id}")
    public String removeItems(@PathVariable("sku_id") Integer skuId,
                             HttpServletRequest request,
                             HttpServletResponse response,
                             Model model){

        try {
            BuyCart buyCart = removeItem(request,response,skuId);
            model.addAttribute("buyCart",buyCart);
            return "/front_page/product/cart";
        } catch (Exception e) {
            e.printStackTrace();
            return "/front_page/product/cart";
        }

    }

}
